package com.atguigu.guli.service.ucenter.controller.api;


import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.helper.JwtHelper;
import com.atguigu.guli.service.base.helper.JwtInfo;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.form.RegisterForm;
import com.atguigu.guli.service.ucenter.entity.form.UserLoginForm;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jdk.nashorn.internal.ir.IfNode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-11-11
 */
@RestController

@RequestMapping("api/ucenter/member")
public class ApiMemberController {
    @Value("${server.port}")
    Integer port;
    @Autowired
    MemberService memberService;

    //远程调用根据日期查询用户的注册数量
    @GetMapping("getMemberRegistCount/{day}")
    public R getMemberRegistCount(@PathVariable String day){
        LambdaQueryWrapper<Member> querywrapper = new LambdaQueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        querywrapper.eq(Member::getGmtCreate,date);
        int count = memberService.count(querywrapper);
        return R.ok().data("count",count);
    }

    //远程调用根据会员id查询会员信息
    @GetMapping("getMemberById/{id}")
    public R getMemberById(@PathVariable String id){
        Member member = memberService.getById(id);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member,memberDto);
        return R.ok().data("item",memberDto);
    }

    //根据订单id查询订单信息
    
@PostMapping("regist")
    public R regist(@RequestBody RegisterForm member){
    //1、注册参数不为空的判断，基本的格式判断
    if(member==null){
        throw new GuliException(ResultCodeEnum.PARAM_ERROR);
    }
    memberService.regist(member);
        return R.ok();
        }
        //根据电话查询用户
    @GetMapping("isRegist/{mobile}")
    public R isRegist(@PathVariable String mobile){
        Integer count = memberService.isRegist(mobile);
        return R.ok().data("item",count>0?"1":"0");
    }

    //用户登录
    @PostMapping("login")
    public R login(@RequestBody UserLoginForm userLoginForm){
    String token  =  memberService.login(userLoginForm);
    return R.ok().data("token",token);
    }

    //根据用户传输的jwt数据解析用户信息
    @GetMapping("auth/getUserInfoByJWT")
    public R getUserInfoByJWT(HttpServletRequest request){
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return R.ok().data("item",jwtInfo);
    }
}

